package cn.dankal.flyaudio.personal;

import android.content.Intent;
import android.view.View;

import com.classic.common.MultipleStatusView;
import com.jeremyliao.liveeventbus.LiveEventBus;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import api.ShopUserServiceFactory;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.base.recyclerview.OnRvItemClickListener;
import cn.dankal.basiclib.pojo.BaseListResponse;
import cn.dankal.basiclib.pojo.BaseResponse;
import cn.dankal.basiclib.pojo.CreateOrderPreviewEntity;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.adapter.OrderRefundAdapter;
import cn.dankal.flyaudio.store.RefundDetailActivity;

public class RefundActivity extends BaseActivity {

    private RecyclerView recyclerView;
    private MultipleStatusView statusView;
    private OrderRefundAdapter orderAdapter;
    private int page = 1;
    private int size = 100;
    public static final String AFTER_SALE = "AFTER_SALE";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_refund;
    }

    @Override
    protected void initComponents() {
        addSingleTitleBar("退款/售后");
        recyclerView = findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        orderAdapter = new OrderRefundAdapter();
        statusView = findViewById(R.id.multiple_status_view);
        recyclerView.setAdapter(orderAdapter);
        orderAdapter.setOnRvItemClickListener(new OnRvItemClickListener<CreateOrderPreviewEntity>() {
            @Override
            public void onItemClick(View v, int position, CreateOrderPreviewEntity data) {
                Intent intent = new Intent(RefundActivity.this, RefundDetailActivity.class);
                intent.putExtra("orderUuid", data.getUuid());
                startActivity(intent);
            }
        });
        LiveEventBus.get("refresh_refund_order",String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                page =1;
                refundOrder();
            }
        });
        refundOrder();
    }

    private void refundOrder() {
        showLoadingDialog();
        ShopUserServiceFactory.getOrders(page, size, AFTER_SALE)
                .subscribe(new AbstractSubscriber<BaseResponse<BaseListResponse<CreateOrderPreviewEntity>>>(this) {
                    @Override
                    public void onNext(BaseResponse<BaseListResponse<CreateOrderPreviewEntity>> baseListResponseBaseResponse) {
                        dismissLoadingDialog();
                        orderAdapter.addDatas(baseListResponseBaseResponse.getData().getList(), true);
                        if(baseListResponseBaseResponse.getData().getList()==null||baseListResponseBaseResponse.getData().getList().size()==0){
                            orderAdapter.clearData();
                            statusView.showEmpty();
                            orderAdapter.notifyDataSetChanged();
                        }else {
                            statusView.showContent();
                            orderAdapter.addDatas(baseListResponseBaseResponse.getData().getList(), true);
                        }
                        page++;
                    }
                });
    }
}
